<?php
//PingBuWang.Com
//Copyright (c) 2017~2099 http://www.pingbuwang.com All rights reserved.
namespace app\admin\controller;

use think\Controller;
use think\Config;
use think\Log;
use think\Session;
use think\Request;
use mip\Init;

class Admin extends Init
{
    protected $beforeActionList = ['start'];

    public function start()
    {
        $this->assign('mipInfoToJson', json_encode(config('mipInfo')));

        // 判断是登陆或忘记密码
        $model = input('model');
        if ($model != 'login' && $model != 'forget_password') {

            $su = session('userInfo');
            if (!$su || !$su['isAdmin'] ) {
                $this->redirect(config('mipInfo')['domain'] . '/' . Config::get('admin') . '/' . 'login', 301);
            }
        }

    }

    public function index()
    {
        $model = input('model');
        $action = input('action');
        $params = input('params');
        $addonsCtr = input('addonsCtr');
        $addonsAct = input('addonsAct');
        $this->assign('model', $model);
        $this->assign('action', $action);
        $id = input('id');
        $this->assign('id', $id);
        $type = input('type');
        $this->assign('type', $type);
        $this->assign('params', $params);

        if (!empty($params)) {
            $params = explode('__', $params);
            if ($params) {
                foreach ($params as $key => $val) {
                    if (strpos($val, '-') !== false) {
                        $tempVal = explode('-', $val);
                        $this->assign($tempVal[0], $tempVal[1]);
                    }
                }
            }
        }

        $this->assign('indexTitle', '核心功能');

        // 处理左侧菜单
        if($model != 'addons') {
            $tempAdminMenu = require_once APP_PATH . '/admin/adminMenu.php';

        }else{
            $tempAdminMenu = array( array('node_name' => '插件管理' ,'node_url' =>'/'.config('admin').'/addons/addons','node_action'=>array('addons/addons' ) ) );

            // 读取插件菜单
            $list = model('app\addons\model\AddonsMenu')->getList();
            foreach ($list  as $k=>$v){
                $arr = array('node_name'=> $v['title'],'node_url'=> '/'.config('admin').'/'.$v['admin_url'] , 'node_action' =>array($v['admin_url']) );
                $tempAdminMenu[] = $arr;
            }

        }

        foreach ($tempAdminMenu as $k1 => $v1) {
            if (isset($v1['children'])) {
                $arr = !isset($v1['node_action']) || $v1['node_action'] == null ? [] : $v1['node_action'];
                foreach ($v1['children'] as $k2 => $v2) {
                    if (isset($v2['node_action'])) {
                        foreach ($v2['node_action'] as $k3 => $strAction) {
                            if (!in_array($strAction, $arr)) {
                                $arr[] = $strAction;
                            }
                        }
                    }

                }
                $tempAdminMenu[$k1]['node_action'] = $arr;
            }
        }
        $this->assign('AdminMenu', $tempAdminMenu);

        $thtml = $this->display(file_get_contents(APP_PATH . '/admin/adminMenu.html'));
        $this->assign('thtml', $thtml);

        $request = Request::instance();

        if (input('model') != 'index.php' && input('model')) {
            if (input('action')) {
                if ($model == 'addons' && $action != 'addons') {
                    $addonsNameSpace = "addons" . "\\" . $action . "\\" . "controller" . "\\" . $addonsCtr;
                    return model($addonsNameSpace)->$addonsAct();
                } else {
                    return $this->fetch(input('model') . '@admin' . '/' . input('action'));
                }
            } else {
                return $this->fetch('admin@' . '/' . input('model'));
            }
        } else {
            return $this->fetch('index@index');
        }

    }

}